package com.shhic.hhplt.server.biz;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.shhic.hhplt.server.model.WorkbayStatisticResult;
import com.shhic.hhplt.server.model.WorkbayStatisticResultDb;

@Component
public class StatisticHelper implements IStatisticHelper{

	@Override
	public List<WorkbayStatisticResult> toWorkbayStatisticResultList(
			List<WorkbayStatisticResultDb> workbayStatisticDbResultList) {
		Map<String,WorkbayStatisticResult> rm = new HashMap<String, WorkbayStatisticResult>();
		for(WorkbayStatisticResultDb w:workbayStatisticDbResultList){
			WorkbayStatisticResult r;
			String workbay = w.getWorkbay();
			if(!rm.containsKey(workbay)){
				r = new WorkbayStatisticResult();
				r.setSuiteName(w.getSuiteName());
				r.setWorkbay(workbay);
				rm.put(workbay, r);
			}else{
				r = rm.get(workbay);
			}
			switch(w.getResult()){
			case FAIL:
				r.setFail(w.getCount());
				break;
			case PASS:
				r.setSuccess(w.getCount());
				break;
			}
		}
		return Arrays.asList(rm.values().toArray(new WorkbayStatisticResult[]{}));
	}

}
